package wangn;

import java.io.*;

final class Helper{

    public String getStringFromFile(String fileName){
        String path = getClass().getResource("/").getPath()+ File.separator+"final_redblack_data"
                + File.separator+fileName;

        StringBuilder sb = new StringBuilder();
        try(BufferedReader br = new BufferedReader(new FileReader(new File(path)))){
            String tmp="";
            while ((tmp = br.readLine())!=null){
                sb.append(tmp);
                sb.append("/n");
            }
        }catch (Exception e){
            System.out.println(e);
        }

        return sb.toString();
    }

    /**
     * 将红黑树打印到out文件中
     */
    public void writeOutFile(RedBlackTree redBlackTree, String outputFileName){
        String path = getClass().getResource("/").getPath()
                + File.separator+outputFileName;
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try(BufferedWriter bw = new BufferedWriter(new FileWriter(file,true))){
            bw.write(redBlackTree.toString());
            bw.flush();
        }catch (Exception e){

        }
    }
}
